@extends('admin.public.header')
@section('title','添加/编辑优惠券')

@section('listcontent')
    <div class="layui-tab-content">
        <form class="layui-form">
            <div class="layui-form layuimini-form">
                <input type="hidden" name="id" value="{{ $info->id ?? '' }}">
                <div class="layui-form-item">
                    <label class="layui-form-label required">优惠券名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="coupon_name" style="width:80%" lay-verify="required" lay-reqtext="优惠券名称不能为空" placeholder="请输入优惠券名称" value="{{ $info->coupon_name ?? '' }}" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">优惠券类型</label>
                    <div class="layui-input-block">
                        <input type="radio" name="coupon_type" value="1" title="满减" @if(($info->coupon_type ?? 1) ==  1) checked @endif  lay-filter="coupon_type">
                        <input type="radio" name="coupon_type" value="2" title="折扣" @if(($info->coupon_type ?? 0) ==  2) checked @endif  lay-filter="coupon_type">
                    </div>
                </div>
                <div class="layui-form-item" style="display: none" id="reduce_price">
                    <label class="layui-form-label required">减免金额</label>
                    <div class="layui-input-block">
                        <input type="number" name="reduce_price" style="width:80%" placeholder="金额不能小于0，可保留两位小数" value="{{$info->reduce_price ?? 0 }}" class="layui-input layui-input-inline">元
                    </div>
                </div>
                <div class="layui-form-item" style="display: none" id="discount">
                    <label class="layui-form-label required">优惠券折扣</label>
                    <div class="layui-input-block">
                        <input type="number" name="discount" style="width:80%" placeholder="优惠券折扣不能小于1折，且不可大于9.9折，可保留两位小数" value="{{$info->discount ?? 0 }}" class="layui-input layui-input-inline">折
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">最低消费金额</label>
                    <div class="layui-input-block">
                        <input type="number" name="min_price" style="width:80%" placeholder="金额不能小于0，无门槛请设为0" value="{{$info->min_price ?? '' }}" class="layui-input layui-input-inline">元
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">发放数量</label>
                    <div class="layui-input-block">
                        <input type="number" name="total_num" placeholder="" style="width:80%" value="{{$info->total_num ?? '' }}" class="layui-input">
                        <tip>优惠券发放数量，没有之后不能领取或发放，-1为不限制发放数量</tip>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">券适用范围</label>
                    <div class="layui-input-block">
                        <input type="radio" name="apply_range" value="1" title="全场通用" @if(($info->apply_range ?? 1) ==  1) checked @endif lay-filter="apply_range">
                        <input type="radio" name="apply_range" value="2" title="指定商品" @if(($info->apply_range ?? 0) ==  2) checked @endif lay-filter="apply_range">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">有效期类型</label>
                    <div class="layui-input-block">
                        <input type="radio" name="expire_type" value="1" title="领取之日起" @if(($info->expire_type ?? 1) ==  1) checked @endif lay-filter="expire_type">
                        <input type="radio" name="expire_type" value="2" title="固定时间" @if(($info->expire_type ?? 0) ==  2) checked @endif lay-filter="expire_type">
                    </div>
                </div>
                <div class="layui-form-item" id="expire_day" style="display: none">
                    <label class="layui-form-label required">领取之日起</label>
                    <div class="layui-input-block">
                        <input type="number" name="expire_day" placeholder="不能小于0，且必须为整数" style="width:80%" value="{{$info->expire_day ?? 7 }}" class="layui-input layui-input-inline">天有效
                    </div>
                </div>
                <div class="layui-form-item" id="expire_time" style="display: none">
                    <label class="layui-form-label">固定时间</label>
                    <div class="layui-input-block">
                        <input type="text" name="start_time" id="start_time" placeholder="请选择起始日期" value="{{ $info->start_time ?? '' }}" class="layui-input layui-input-inline" style="width:37%">
                        <label class="layui-form-label" style="width:0;text-align: center">-</label>
                        <input type="text" name="end_time" id="end_time" placeholder="请选择截止日期" value="{{ $info->end_time ?? ''}}" class="layui-input layui-input-inline"style="width:40%">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">状态</label>
                    <div class="layui-input-block">
                        <input type="radio" name="status" value="1" title="显示" @if(($info->status ?? 1) ==  1)checked @endif>
                        <input type="radio" name="status" value="0" title="隐藏" @if(($info->status ?? '') ==  0)checked @endif>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">首页展示</label>
                    <div class="layui-input-block">
                        <input type="radio" name="index_status" value="1" title="显示" @if(($info->index_status ?? 1) ==  1)checked @endif>
                        <input type="radio" name="index_status" value="0" title="隐藏" @if(($info->index_status ?? '') ==  0)checked @endif>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">优惠券描述</label>
                    <div class="layui-input-block">
                        <textarea name="describe" style="width:80%" placeholder="请输入等优惠券描述" class="layui-textarea">{{ $info->describe ?? '' }}</textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">排序</label>
                    <div class="layui-input-block">
                        <input type="number" name="sort" style="width:80%" value="{{$info->sort ?? 100 }}" class="layui-input">
                        <tip>数字越小越靠前</tip>
                    </div>
                </div>
            </div>
            <div class="hr-line"></div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" id="saveBtn" lay-submit lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
        </form>
    </div>

@endsection

@section('listscript')
    <script type="text/javascript">
        layui.use(['layer','form','laydate'], function () {
            var layer = layui.layer, form = layui.form, $=layui.jquery ,laydate = layui.laydate;
            //日期
            laydate.render({
                elem: '#start_time'
                ,theme: '#393D49'
                ,festival: true //显示节日
                ,istime: false
                ,choose: function(datas){ //选择日期完毕的回调
                    compare_time($('#start_time').val(),$('#end_time').val());
                }
            });
            laydate.render({
                elem: '#end_time'
                ,theme: '#393D49'
                ,festival: true //显示节日
                ,istime: false
                ,choose: function(datas){ //选择日期完毕的回调
                    compare_time($('#start_time').val(),$('#end_time').val());
                }
            });
            $(function (){
                var coupon_type = $("input[name=coupon_type]").val();
                var coupon_type = <? echo $info->coupon_type ?? 1; ?>;
                var expire_type = $("input[name=expire_type]").val();
                var apply_range = $("input[name=apply_range]").val();

                if(coupon_type == 1){
                    $("#discount").hide();
                    $("#reduce_price").show();
                } else if(coupon_type == 2){
                    $("#discount").show();
                    $("#reduce_price").hide();
                }

                if(expire_type == 1){
                    $("#expire_day").show();
                    $("#expire_time").hide();
                } else if(expire_type == 2){
                    $("#expire_day").hide();
                    $("#expire_time").show();
                }

                if(apply_range == 1){

                } else if(apply_range == 2){

                }
            });
            form.on('submit(saveBtn)', function(data){
                var loading = layer.msg('加载中..', {icon: 16,shade: 0.3,time: false});
                $.ajax({
                    url:'/admin/coupon/edit-detail',
                    type:'post',
                    data: data.field,
                    dataType:"JSON",
                    headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
                    success:function(res){
                        layer.close(loading);  //返回数据关闭loading
                        if(res.code == 0){
                            layer.msg(res.message, {icon: 1},function(){
                                setTimeout('parent.location.reload()',500);
                            });
                        }
                        else{
                            layer.msg(res.message,{icon: 2});
                        }
                    },
                    error:function(e){
                        layer.close(loading);
                        layer.msg("提交失败",{icon: 2});
                    },
                });
                return false;
            });

            form.on('radio(coupon_type)', function (data) {
                var coupon_type = data.value;
                if(coupon_type == 1){
                    $("#discount").hide();
                    $("#reduce_price").show();
                } else if(coupon_type == 2){
                    $("#discount").show();
                    $("#reduce_price").hide();
                }
            });

            form.on('radio(expire_type)', function (data) {
                var expire_type = data.value;
                if(expire_type == 1){
                    $("#expire_time").hide();
                    $("#expire_day").show();
                } else if(expire_type == 2){
                    $("#expire_time").show();
                    $("#expire_day").hide();
                }
            });

            form.on('radio(apply_range)', function (data) {
                var apply_range = data.value;

            });
        });
    </script>
@endsection

